<?php
require_once 'constants.php';
require_once 'db.php';

$id = $_GET['img_id'];
$newWidth = $_GET['width'];
$newHeight = $_GET['height'];

$row = dbGetImageById($id);
$img = imagecreatefromstring($row['image']);
$width = imagesx($img);
$height = imagesy($img);
$ratioW = $newWidth / $width;
$ratioH = $newHeight / $height;
$ratio = min($ratioW, $ratioH);

$newImg = imagecreatetruecolor($width * $ratio, $height * $ratio);
imagecopyresampled($newImg, $img, 0, 0, 0, 0, $width * $ratio, $height * $ratio, $width, $height);

//Capure image as a BIN variable
ob_start();
imagejpeg($newImg);
$imgBin = ob_get_contents();
ob_end_clean();
dbUpdateImageById($id, addslashes($imgBin));

header('content-type: image/jpg');
imagejpeg($newImg);
?>
